'''
Author: hua
Date: 2021-01-26 20:04:37
description: 
LastEditors: hua
LastEditTime: 2021-02-04 21:47:42
'''
from flask import Flask, request
from flask import render_template
import platform,os,re
app = Flask(__name__)
data = {}

def compared_version(ver1, ver2):
    """
    传入不带英文的版本号,特殊情况："10.12.2.6.5">"10.12.2.6"
    :param ver1: 版本号1
    :param ver2: 版本号2
    :return: ver1< = >ver2返回-1/0/1
    """
    list1 = str(ver1).split(".")
    list2 = str(ver2).split(".")
    print(list1)
    print(list2)
    # 循环次数为短的列表的len
    for i in range(len(list1)) if len(list1) < len(list2) else range(len(list2)):
        if int(list1[i]) == int(list2[i]):
            pass
        elif int(list1[i]) < int(list2[i]):
            return -1
        else:
            return 1
    # 循环结束，哪个列表长哪个版本号高
    if len(list1) == len(list2):
        return True
    elif len(list1) < len(list2):
        return False
    else:
        return True

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/install")
def install():
    step = request.args.get('step')
    if step == '2':
        data['platform'] = platform.platform().split("-")[0]
        try:
            resultNginx = os.popen("nginx -v")
            # nginx version: nginx/1.15.10
            resultNginx = resultNginx.read().splitlines()[0]
            data['install_nginx'] = resultNginx.split(" ")[1]
            nginxVersion = resultNginx
            data['install_nginx_warn'] = True if compared_version(nginxVersion, "1.6.0") else False
        except:
            data['install_nginx'] = '0'
            nginxVersion = '0'
            data['install_nginx_warn'] = True if compared_version(nginxVersion, "1.6.0") else False
        try:
            resultMysql = os.popen("mysql -V")
            # mysql  Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1
            resultMysql = resultMysql.read().splitlines()[0]
            data['install_mysql'] = re.search(r'Distrib (.*),', resultMysql).group(0).strip("Distrib").strip(",").strip(" ")
            mysqlVersion = resultMysql
            data['install_mysql_warn'] = True if compared_version(mysqlVersion, "5.1.0") else False
        except:
            data['install_mysql'] = '0'
            mysqlVersion = '0'
            data['install_mysql_warn'] = True if compared_version(mysqlVersion, "5.1.0") else False

        try:
            resultNode = os.popen("node -v")
            resultNode = resultNode.read().splitlines()[0]
            data['install_node'] = resultNode.replace("v", "")
            nodeVersion = resultNode
            data['install_node_warn'] = True if compared_version(nodeVersion, "14.4.0") else False
        except:
            data['install_node'] = '0'
            resultNode = '0'
            data['install_node_warn'] = True if compared_version(resultNode, "14.4.0") else False
        try:
            resultRedis = os.popen("redis-server -v")
            # redis-cli 4.0.9
            resultRedis = resultRedis.read().splitlines()[0]
            data['install_redis'] = resultRedis
            redisVersion = resultRedis.split(" ")[1]
            data['install_redis_warn'] = True if compared_version(redisVersion, "4.0.0") else False
        except:
            data['install_redis'] = '0'
            resultNode = '0'
            data['install_redis_warn'] = True if compared_version(resultNode, "4.0.0") else False
        try:
            resultPy = os.popen("python -V")
            resultPy = resultPy.read().splitlines()[0]
            data['install_py'] = resultPy
            pyVersion = resultPy.split(" ")[1]
            data['install_py_warn'] = True if compared_version(pyVersion, "3.5.0") else False
        except Exception as e:
            print(e)
            data['install_py'] = '0'
            resultPy = '0'
            data['install_py_warn'] = True if compared_version(resultPy, "3.5.0") else False
    return render_template("install.html", step=step, data=data)

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0', port=81)